﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Jermaine.UI
{
    public class UIInvoker
    {
        private UIInvoker() { }

        /// <summary>
        /// 无参数异步更新UI
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="Action">执行动作</param>
        public static void AsyncCall(Control control, Action Action) {
            if (control.InvokeRequired) {
                control.Invoke(Action);
            } else {
                Action();
            }
        }

        /// <summary>
        /// 一个参数更新UI
        /// </summary>
        /// <typeparam name="TArgs">参数</typeparam>
        /// <param name="control">控件</param>
        /// <param name="Action">执行动作</param>
        /// <param name="args">参数</param>
        public static void AsyncCall<TArgs>(Control control, Action<TArgs> Action, TArgs args) {
            if (control.InvokeRequired) {
                control.Invoke(Action, args);
            } else {
                Action(args);
            }
        }

        /// <summary>
        /// 两个参数更新UI
        /// </summary>
        /// <typeparam name="TArgs1">参数1</typeparam>
        /// <typeparam name="TArgs2">参数2</typeparam>
        /// <param name="control">控件</param>
        /// <param name="Action">执行动作</param>
        /// <param name="args1">参数1</param>
        /// <param name="args2">参数2</param>
        public static void AsyncCall<TArgs1, TArgs2>(Control control, Action<TArgs1, TArgs2> Action, TArgs1 args1, TArgs2 args2) {
            if (control.InvokeRequired) {
                control.Invoke(Action, args1, args2);
            } else {
                Action(args1, args2);
            }
        }

        /// <summary>
        /// 三个参数更新UI
        /// </summary>
        /// <typeparam name="TArgs1">参数1</typeparam>
        /// <typeparam name="TArgs2">参数2</typeparam>
        /// <typeparam name="TArgs3">参数3</typeparam>
        /// <param name="control">控件</param>
        /// <param name="Action">执行动作</param>
        /// <param name="args1">参数1</param>
        /// <param name="args2">参数2</param>
        /// <param name="args3">参数3</param>
        public static void AsyncCall<TArgs1, TArgs2, TArgs3>(Control control, Action<TArgs1, TArgs2, TArgs3> Action, TArgs1 args1, TArgs2 args2, TArgs3 args3) {
            if (control.InvokeRequired) {
                control.Invoke(Action, args1, args2, args3);
            } else {
                Action(args1, args2, args3);
            }
        }

        /// <summary>
        /// 四个参数更新UI
        /// </summary>
        /// <typeparam name="TArgs1">参数1</typeparam>
        /// <typeparam name="TArgs2">参数2</typeparam>
        /// <typeparam name="TArgs3">参数3</typeparam>
        /// <typeparam name="TArgs4">参数4</typeparam>
        /// <param name="control">控件</param>
        /// <param name="Action">执行动作</param>
        /// <param name="args1">参数1</param>
        /// <param name="args2">参数2</param>
        /// <param name="args3">参数3</param>
        /// <param name="args4">参数4</param>
        public static void AsyncCall<TArgs1, TArgs2, TArgs3, TArgs4>(Control control, Action<TArgs1, TArgs2, TArgs3, TArgs4> Action, TArgs1 args1, TArgs2 args2, TArgs3 args3, TArgs4 args4) {
            if (control.InvokeRequired) {
                control.Invoke(Action, args1, args2, args3, args4);
            } else {
                Action(args1, args2, args3, args4);
            }
        }

        /// <summary>
        /// 后台线程执行任务
        /// </summary>
        /// <param name="start">执行动作</param>
        public static void RunBackgroundWork(ThreadStart start) {
            new Thread(start) { IsBackground = true }.Start();
        }
    }
}
